package janelas;

import outros.CampoMask;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import outros.Documento;
import outros.GeradorRelatorio;
import outros.Util;
import persistencia.DAOEmprestimo;
import persistencia.DAOGenerico;
import persistencia.DAOPatrimonio;
import persistencia.DAOSetor;
import principal.Principal;
import transporte.TOEmprestimo;
import transporte.TOGenerico;
import transporte.TOPatrimonio;
import transporte.TOSetor;

public class Emprestimo extends javax.swing.JInternalFrame implements JanelaPai {
    private ArrayList<TOSetor> setor = new ArrayList();
    private DAOSetor daoSetor = new DAOSetor();
    private ArrayList<TOGenerico> orgao = new ArrayList();
    private DAOGenerico daoOrgao = new DAOGenerico();
    private TOPatrimonio patrimonio = null;
    private DAOPatrimonio daoPatrimonio = new DAOPatrimonio();
    private DAOEmprestimo daoEmprestimo = new DAOEmprestimo();
    //
    private GeradorRelatorio gr = new GeradorRelatorio();
    private Util util = new Util();
    private TOPatrimonio[] escolhidos = new TOPatrimonio[]{
                                        new TOPatrimonio(),new TOPatrimonio(),
                                        new TOPatrimonio(),new TOPatrimonio(),
                                        new TOPatrimonio(),new TOPatrimonio(),
                                        new TOPatrimonio(),new TOPatrimonio()};

    public Emprestimo() {
        for (int i=0;i<escolhidos.length;i+=1) {
            escolhidos[i].setNumero(-1);
        }
        initComponents();
        tabela.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        orgao = daoOrgao.selecionarGenerico("orgao", "nome");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-getWidth())/2, (int)((screenSize.height-getHeight())/3)-25, getWidth(), getHeight());
        preencherComboBox();
        alternarTipoEmprestimo(false, false);
        radioTipoInterno.setEnabled(false);
        radioTipoExterno.setEnabled(false);
    }

    public void reiniciar() {        
        camOrgao.removeAllItems();
        preencherComboBox();
        limparCampos();
        alternarTipoEmprestimo(true, false);
    }

    private void alternarTipoEmprestimo(boolean interno, boolean externo) {
        camSetor.setEnabled(interno);
        camObs.setEnabled(interno);
        camEmpresa.setEnabled(externo);
        camCnpj.setEnabled(externo);
        camEndereco.setEnabled(externo);
        camTelefone.setEnabled(externo);
        camResponsavel.setEnabled(externo);
    }

    private void limparCampos() {
        for (int i=0;i<escolhidos.length;i+=1) {
            tabela.setValueAt("", i, 0);
            escolhidos[i].setNumero(-1);
        }
        camSetor.setSelectedIndex(0);
        camOrgao.setSelectedIndex(0);
        camBuscaArea.setText("");
        camBuscaGrupo.setText("");
        camBuscaSubgrupo.setText("");
        camBuscaNumero.setText("");
        camEmpresa.setText("");
        camCnpj.setText("");
        camEndereco.setText("");
        camTelefone.setText("");
        camResponsavel.setText("");
        camPatrimonio.setText("");
        camObs.setText("");
        camPatrimonio.setText("");
        camBuscaArea.requestFocus();
    }

    private void preencherComboBox() {
        setor = daoSetor.selecionar("");
        camSetor.removeAllItems();
        for (int i=0;i<setor.size();i+=1) {
            camSetor.addItem(setor.get(i).getNome());
        }
        for (int i=0;i<orgao.size();i+=1) {
            camOrgao.addItem(orgao.get(i).getNome());
        }
    }

    private void buscarPatrimonio() {
        int[] valores = verificarCampos();
        for (int i=0;i<valores.length;i+=1) {
            if (valores[i]==-1) {
                JOptionPane.showMessageDialog(null, "Preencha todos os campos.");
                return;
            }
        }
        patrimonio = daoPatrimonio.selecionarRegistro(valores[0], valores[1], valores[2], valores[3], valores[4], -1);
        if (patrimonio!=null) {
            if (camTipo.getSelectedIndex()==0) {
                if (patrimonio.getEmprestimo()==0) {
                    JOptionPane.showMessageDialog(null, "O patrimônio não está emprestado.", "AVISO", JOptionPane.WARNING_MESSAGE);
                    patrimonio = null;
                    camPatrimonio.setText("");
                    return;
                }
            } else {
                if (patrimonio.getGarantia()==1 || patrimonio.getEmprestimo()==1) {
                    JOptionPane.showMessageDialog(null, "O patrimônio já está emprestado ou está na garantia.", "AVISO", JOptionPane.WARNING_MESSAGE);
                    patrimonio = null;
                    camPatrimonio.setText("");
                    return;
                }
            }
            camPatrimonio.setText(patrimonio.getNome());
        } else {
            JOptionPane.showMessageDialog(null, "Não foi possível encontrar nenhum patrimônio.");
            camPatrimonio.setText("");
        }
    }

    private int[] verificarCampos() {
        int[] valores = new int[5];
        if (camBuscaArea.getText().length()>0) {
            valores[0] = Integer.parseInt(camBuscaArea.getText());
        } else {
            valores[0] = -1;
        }
        if (camBuscaGrupo.getText().length()>0) {
            valores[1] = Integer.parseInt(camBuscaGrupo.getText());
        } else {
            valores[1] = -1;
        }
        if (camBuscaSubgrupo.getText().length()>0) {
            valores[2] = Integer.parseInt(camBuscaSubgrupo.getText());
        } else {
            valores[2] = -1;
        }
        if (camBuscaNumero.getText().length()>0) {
            valores[3] = Integer.parseInt(camBuscaNumero.getText());
        } else {
            valores[3] = -1;
        }
        valores[4] = orgao.get(camOrgao.getSelectedIndex()).getNumero();
        return valores;
    }

    private String[] getParametros() {
        String[] parametros = new String[8];
        Date data =  new Date();
        Locale local = new Locale("pt","BR");
        DateFormat dateFormat;
        dateFormat = new SimpleDateFormat("'Goiânia,' dd 'de' MMMM 'de' yyyy'.'",local);
        parametros[0] = dateFormat.format(data);
        parametros[1] = (String)camTipo.getSelectedItem();
        if (radioTipoExterno.isSelected()) {
            //dateFormat = new SimpleDateFormat("dd/MM/yyyy", local);
            parametros[2] = "";
            parametros[3] = camEmpresa.getText();
            parametros[4] = camCnpj.getText();
            parametros[5] = camEndereco.getText();
            parametros[6] = camTelefone.getText();
            parametros[7] = camResponsavel.getText();
        } else {
            parametros[2] = (String)camSetor.getSelectedItem();
            parametros[3] = daoSetor.getAtributo(setor.get(camSetor.getSelectedIndex()).getNumero(), "ramal");
            parametros[4] = daoSetor.getAtributo(setor.get(camSetor.getSelectedIndex()).getNumero(), "chefe");
            parametros[5] = camObs.getText();
        }
        return parametros;
    }

    private boolean jaEstaNaTabela(int[] dados) {
        for (int i=0;i<escolhidos.length;i+=1) {
            if (escolhidos[i].getNumero()!=-1) {
                if (escolhidos[i].getOrgao()==dados[0] && escolhidos[i].getArea()==dados[1] &&
                    escolhidos[i].getGrupo()==dados[2] && escolhidos[i].getSubgrupo()==dados[3] &&
                    escolhidos[i].getNumero()==dados[4]) {
                    return true;
                }
            }
        }
        return false;
    }

    private void adicionarPatrimonio(String pat) {
        if (!jaEstaNaTabela(new int[]{patrimonio.getOrgao(), patrimonio.getArea(),
                                     patrimonio.getGrupo(), patrimonio.getSubgrupo(),
                                     patrimonio.getNumero()})) {
            for (int i=0;i<escolhidos.length;i+=1) {
                if (escolhidos[i].getNumero()==-1) {
                    escolhidos[i].setOrgao(patrimonio.getOrgao());
                    escolhidos[i].setArea(patrimonio.getArea());
                    escolhidos[i].setGrupo(patrimonio.getGrupo());
                    escolhidos[i].setSubgrupo(patrimonio.getSubgrupo());
                    escolhidos[i].setNumero(patrimonio.getNumero());
                    escolhidos[i].setNome(pat);
                    tabela.setValueAt(pat, i, 0);
                    break;
                } else if (i==escolhidos.length-1) {
                    JOptionPane.showMessageDialog(null, "Todos os espaços para patrimônio estão cheios.");
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "O patrimônio já está na lista.", "AVISO", JOptionPane.WARNING_MESSAGE);
        }
    }

    private void removerPatrimonio(int linha) {
        tabela.setValueAt("", linha, 0);
        escolhidos[linha].setNumero(-1);
    }

    private ArrayList<TOEmprestimo> getEmprestimos() {
        ArrayList<TOEmprestimo> emp = new ArrayList();
        TOEmprestimo e = null;
        for (int i=0;i<escolhidos.length;i+=1) {
            if (escolhidos[i].getNumero()!=-1) {
                e = new TOEmprestimo();
                e.setData(util.getData());
                e.setHora(util.getHora());
                e.setUsuario(Principal.getUsuarioLogado().getNumero());
                e.setTipo(camTipo.getSelectedIndex());
                if (radioTipoInterno.isSelected()) {
                    e.setResponsavel(daoSetor.getAtributo(setor.get(camSetor.getSelectedIndex()).getNumero(), "chefe"));
                    e.setObs(camObs.getText());
                    e.setSetorResponsavel(setor.get(camSetor.getSelectedIndex()).getNumero());
                } else {
                    e.setResponsavel(camResponsavel.getText());
                    e.setEmpresa(camEmpresa.getText());
                    e.setEndereco(camEndereco.getText());
                    //e.setCnpj(camCnpj.getText().replaceAll("\\.", "").replaceAll("-", "").replaceAll("/", ""));
                    //e.setTelefone(camTelefone.getText().replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("-", ""));
                    e.setCnpj(camCnpj.getText());
                    e.setTelefone(camTelefone.getText());
                }
                e.setPatrimonio(escolhidos[i]);
                emp.add(e);
            }
        }
        return emp;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupoTipoEmprestimo = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel28 = new javax.swing.JLabel();
        camBuscaArea = new javax.swing.JTextField();
        jLabel25 = new javax.swing.JLabel();
        camBuscaGrupo = new javax.swing.JTextField();
        jLabel26 = new javax.swing.JLabel();
        camBuscaSubgrupo = new javax.swing.JTextField();
        jLabel27 = new javax.swing.JLabel();
        camBuscaNumero = new javax.swing.JTextField();
        jLabel29 = new javax.swing.JLabel();
        camOrgao = new javax.swing.JComboBox();
        botBuscar = new javax.swing.JButton();
        camPatrimonio = new javax.swing.JTextField();
        botAdicionar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        camEmpresa = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        camCnpj = new CampoMask("##.###.###/####-##");
        jLabel10 = new javax.swing.JLabel();
        camEndereco = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        camTelefone = new CampoMask("(##)####-####");
        jLabel12 = new javax.swing.JLabel();
        camResponsavel = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        camObs = new javax.swing.JTextArea();
        jLabel30 = new javax.swing.JLabel();
        camSetor = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        camTipo = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        radioTipoInterno = new javax.swing.JRadioButton();
        radioTipoExterno = new javax.swing.JRadioButton();
        botLimpar = new javax.swing.JButton();
        botBuscar2 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Movimentar Bem - Transferência");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Buscar Patrimônio", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 0, 13))); // NOI18N

        jLabel28.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel28.setText("Área");

        camBuscaGrupo.setDocument(new Documento(true, 5));
        camBuscaArea.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel25.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel25.setText("Grupo");

        camBuscaGrupo.setDocument(new Documento(true, 5));
        camBuscaGrupo.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel26.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel26.setText("Subgrupo");

        camBuscaSubgrupo.setDocument(new Documento(true, 5));
        camBuscaSubgrupo.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel27.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel27.setText("Número");

        camBuscaNumero.setDocument(new Documento(true, 5));
        camBuscaNumero.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camBuscaNumero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camBuscaNumeroActionPerformed(evt);
            }
        });

        jLabel29.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel29.setText("Órgão");

        camOrgao.setFont(new java.awt.Font("Segoe UI", 0, 13));

        botBuscar.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botBuscar.setText("Buscar");
        botBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botBuscarActionPerformed(evt);
            }
        });

        camBuscaGrupo.setDocument(new Documento(true, 5));
        camPatrimonio.setEditable(false);
        camPatrimonio.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camPatrimonio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camPatrimonioActionPerformed(evt);
            }
        });

        botAdicionar.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botAdicionar.setText("Adicionar");
        botAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botAdicionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel29)
                            .addComponent(camOrgao, 0, 319, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(camBuscaArea, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel28))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel25)
                            .addComponent(camBuscaGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel26)
                            .addComponent(camBuscaSubgrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(camBuscaNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botBuscar))
                            .addComponent(jLabel27)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(camPatrimonio, javax.swing.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botAdicionar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel29)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camOrgao, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel27)
                                .addComponent(jLabel26))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel28)
                                .addComponent(jLabel25)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(camBuscaSubgrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(camBuscaNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(camBuscaGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(camBuscaArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(botBuscar))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(camPatrimonio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botAdicionar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Movimentação", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 0, 13))); // NOI18N

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Bens"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tabelaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);
        tabela.getColumnModel().getColumn(0).setResizable(false);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Empréstimo Externo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 0, 13))); // NOI18N

        jLabel8.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel8.setText("Empresa");

        camEmpresa.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel9.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel9.setText("CNPJ");

        camCnpj.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camCnpj.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel10.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel10.setText("Endereço");

        camEndereco.setFont(new java.awt.Font("Segoe UI", 0, 13));

        jLabel11.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel11.setText("Telefone");

        camTelefone.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camTelefone.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel12.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel12.setText("Responsável");

        camResponsavel.setFont(new java.awt.Font("Segoe UI", 0, 13));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(camEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(camCnpj, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel10)
                    .addComponent(camEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(camResponsavel, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(camTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(camEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camResponsavel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Empréstimo Interno", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 0, 13))); // NOI18N

        jLabel13.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel13.setText("Observações");

        camObs.setColumns(20);
        camObs.setRows(5);
        jScrollPane2.setViewportView(camObs);

        jLabel30.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel30.setText("Setor Responsável");

        camSetor.setFont(new java.awt.Font("Segoe UI", 0, 13));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                    .addComponent(jLabel30)
                    .addComponent(camSetor, 0, 255, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel30)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(camSetor, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jLabel6.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel6.setText("Tipo");

        camTipo.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        camTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Entrada na Agecom", "Saída da Agecom" }));
        camTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camTipoActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tipo de Empréstimo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 0, 13))); // NOI18N

        grupoTipoEmprestimo.add(radioTipoInterno);
        radioTipoInterno.setSelected(true);
        radioTipoInterno.setText("Interno");
        radioTipoInterno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioTipoInternoActionPerformed(evt);
            }
        });

        grupoTipoEmprestimo.add(radioTipoExterno);
        radioTipoExterno.setText("Externo");
        radioTipoExterno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioTipoExternoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(radioTipoInterno)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(radioTipoExterno))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(radioTipoExterno)
                .addComponent(radioTipoInterno))
        );

        botLimpar.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botLimpar.setText("Limpar Campos");
        botLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botLimparActionPerformed(evt);
            }
        });

        botBuscar2.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botBuscar2.setText("OK");
        botBuscar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botBuscar2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(camTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botLimpar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botBuscar2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(camTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botBuscar2)
                    .addComponent(botLimpar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void camBuscaNumeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camBuscaNumeroActionPerformed
        buscarPatrimonio();
}//GEN-LAST:event_camBuscaNumeroActionPerformed

    private void botBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botBuscarActionPerformed
        buscarPatrimonio();
}//GEN-LAST:event_botBuscarActionPerformed

    private void camPatrimonioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camPatrimonioActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_camPatrimonioActionPerformed

    private void botAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botAdicionarActionPerformed
        if (camPatrimonio.getText().length()>0) {
            adicionarPatrimonio(camPatrimonio.getText());
        } else {
            JOptionPane.showMessageDialog(null, "Nenhum patrimônio selecionado.");
        }
}//GEN-LAST:event_botAdicionarActionPerformed

    private void botBuscar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botBuscar2ActionPerformed
        ArrayList<TOEmprestimo> e = getEmprestimos();
        if (e.size()>0) {
            if (camTipo.getSelectedIndex()==1) {
                if (radioTipoInterno.isSelected()) {
                    if (JOptionPane.showConfirmDialog(null, "Confirma o empréstimo interno?")==0) {
                        daoEmprestimo.criarRegistro(e, 1);
                        gr.geraTermoEmpInterno(getParametros(), escolhidos);
                        limparCampos();
                    }
                } else {
                    if (JOptionPane.showConfirmDialog(null, "Confirma o empréstimo externo?")==0) {
                        daoEmprestimo.criarRegistro(e, 1);
                        gr.geraTermoEmpExterno(getParametros(), escolhidos);
                        limparCampos();
                    }
                }
            } else {
                if (JOptionPane.showConfirmDialog(null, "Confirma o retorno do empréstimo?")==0) {
                    daoEmprestimo.criarRegistro(e, 0);
                    limparCampos();
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Selecione pelo menos um item para ser emprestado.");
        }
    }//GEN-LAST:event_botBuscar2ActionPerformed

    private void tabelaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabelaKeyReleased
        if (evt.getKeyCode()==KeyEvent.VK_DELETE) {
            removerPatrimonio(tabela.getSelectedRow());
        }
    }//GEN-LAST:event_tabelaKeyReleased

    private void botLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botLimparActionPerformed
        limparCampos();
}//GEN-LAST:event_botLimparActionPerformed

    private void camTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camTipoActionPerformed
        if (camTipo.getSelectedIndex()==0) {
            alternarTipoEmprestimo(false, false);
            radioTipoInterno.setEnabled(false);
            radioTipoExterno.setEnabled(false);
        } else {
            radioTipoInterno.setSelected(true);
            alternarTipoEmprestimo(true, false);
            radioTipoInterno.setEnabled(true);
            radioTipoExterno.setEnabled(true);
        }
        limparCampos();
    }//GEN-LAST:event_camTipoActionPerformed

    private void radioTipoExternoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioTipoExternoActionPerformed
        alternarTipoEmprestimo(false, true);
    }//GEN-LAST:event_radioTipoExternoActionPerformed

    private void radioTipoInternoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioTipoInternoActionPerformed
        alternarTipoEmprestimo(true, false);
    }//GEN-LAST:event_radioTipoInternoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botAdicionar;
    private javax.swing.JButton botBuscar;
    private javax.swing.JButton botBuscar2;
    private javax.swing.JButton botLimpar;
    private javax.swing.JTextField camBuscaArea;
    private javax.swing.JTextField camBuscaGrupo;
    private javax.swing.JTextField camBuscaNumero;
    private javax.swing.JTextField camBuscaSubgrupo;
    private javax.swing.JTextField camCnpj;
    private javax.swing.JTextField camEmpresa;
    private javax.swing.JTextField camEndereco;
    private javax.swing.JTextArea camObs;
    private javax.swing.JComboBox camOrgao;
    private javax.swing.JTextField camPatrimonio;
    private javax.swing.JTextField camResponsavel;
    private javax.swing.JComboBox camSetor;
    private javax.swing.JTextField camTelefone;
    private javax.swing.JComboBox camTipo;
    private javax.swing.ButtonGroup grupoTipoEmprestimo;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton radioTipoExterno;
    private javax.swing.JRadioButton radioTipoInterno;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables

}
